package com.sgu6.middleware.util;


import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class HttpUtil {

    public HttpUtil() {
    }

    public static String get(String path){
        try {
            URL url = new URL(path.trim());
            //打开连接
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            if(200 == urlConnection.getResponseCode()){
                //得到输入流
                InputStream is =urlConnection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while(-1 != (len = is.read(buffer))){
                    baos.write(buffer,0,len);
                    baos.flush();
                }
                return baos.toString("utf-8");
            }
        }  catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static InputStream getImageInputStream(String url) {
        URL imgUrl = null;
        InputStream is = null;

        try {
            imgUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imgUrl.openConnection();
            conn.setDoInput(true);
            conn.connect();
            is = conn.getInputStream();
            InputStream var4 = is;
            return var4;
        } catch (MalformedURLException var16) {
            var16.printStackTrace();
        } catch (IOException var17) {
            var17.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException var15) {
                var15.printStackTrace();
            }

        }

        return null;
    }

    public static String post(String urlString, String params) {
        URL url = null;
        HttpURLConnection httpConn = null;

        try {
            url = new URL(urlString);
            httpConn = (HttpURLConnection)url.openConnection();
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setUseCaches(false);
            httpConn.setRequestMethod("POST");
            httpConn.setConnectTimeout(30000);
            httpConn.setRequestProperty("Content-Type", "application/json");
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            httpConn.setRequestProperty("Charset", "UTF-8");
            httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            httpConn.connect();
            DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
            if (StringUtils.isNotEmpty(params)) {
                dos.writeBytes(URLEncoder.encode(params, "UTF-8"));
            }

            dos.flush();
            dos.close();
            int resultCode = httpConn.getResponseCode();
            if (200 == resultCode) {
                StringBuffer sb = new StringBuffer();
                BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), StandardCharsets.UTF_8));

                String readLine;
                while((readLine = responseReader.readLine()) != null) {
                    sb.append(readLine).append("\n");
                }

                responseReader.close();
                return sb.toString();
            }
        } catch (MalformedURLException var9) {
            var9.printStackTrace();
        } catch (IOException var10) {
            var10.printStackTrace();
        }

        return null;
    }

    @SuppressWarnings("unchecked")
    public static String postForHeader(String urlString, String params, Map<String, String> header) {
        URL url = null;
        HttpURLConnection httpConn = null;

        try {
            url = new URL(urlString);
            httpConn = (HttpURLConnection)url.openConnection();
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setUseCaches(false);
            httpConn.setRequestMethod("POST");
            httpConn.setConnectTimeout(30000);
            httpConn.setRequestProperty("Content-Type", "application/json");
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            httpConn.setRequestProperty("Charset", "UTF-8");
            httpConn.setRequestProperty("Connection", "close");
            httpConn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            Iterator var5 = header.entrySet().iterator();

            while(var5.hasNext()) {
                Entry<String, String> item = (Entry)var5.next();
                httpConn.setRequestProperty(item.getKey(), item.getValue());
            }

            httpConn.connect();
            DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream());
            if (StringUtils.isNotEmpty(params)) {
                dos.writeBytes(URLEncoder.encode(params, "UTF-8"));
            }

            dos.flush();
            dos.close();
            int resultCode = httpConn.getResponseCode();
            if (200 == resultCode) {
                StringBuffer sb = new StringBuffer();
                BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), StandardCharsets.UTF_8));

                String readLine;
                while((readLine = responseReader.readLine()) != null) {
                    sb.append(readLine).append("\n");
                }

                responseReader.close();
                return sb.toString();
            }
        } catch (MalformedURLException var10) {
            var10.printStackTrace();
        } catch (IOException var11) {
            var11.printStackTrace();
        }

        return null;
    }
}
